package br.ucb.musicbay.validator;

import java.util.Locale;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import br.ucb.musicbay.usuario.UsuarioBO;

@FacesValidator("validatorEmail")
public class EmailValidator implements Validator {

	@Override
	public void validate(FacesContext context, UIComponent comp, Object object) throws ValidatorException {
		UsuarioBO usuarioBo = new UsuarioBO();
		String email = object.toString();
		FacesMessage message;
		if (usuarioBo.verificaItem("email", email) != null) {
			if(context.getViewRoot().getLocale().equals(new Locale("pt_BR")))
				message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "E-mail nao disponivel",
					"O E-mail informado ja se encontra cadastrado no sistema");
			else
				message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "E-mail not available",
						"The E-mail informed is already registered in the system");
			throw new ValidatorException(message);
		}
		
	}

}
